package org.chen.common.flight;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@TableName("flight_roundtrip_flight_data")
public class RoundTripFlightData {
    private Long id;
    private String transactionId;
    private String searchRoute;
    private String itineraryId;

    // 价格信息
    private BigDecimal adultTotalPrice;
    private BigDecimal adultPrice;
    private BigDecimal adultTax;
    private BigDecimal childTotalPrice;
    private BigDecimal childPrice;
    private BigDecimal childTax;
    private BigDecimal infantTotalPrice;
    private BigDecimal infantPrice;
    private BigDecimal infantTax;
    private String currency;
    private Integer miseryIndex;
    private Integer ticketCount;
    
    // 去程信息
    private String outboundFlightNumber;
    private String outboundDepartureTime;
    private String outboundArrivalTime;
    private String outboundDuration;
    private Integer outboundTransferCount;
    private Boolean outboundIsDirectFlight;
    private String outboundDepartureAirportCode;
    private String outboundArrivalAirportCode;
    private String outboundDepartureCityCode;
    private String outboundArrivalCityCode;
    
    // 返程信息
    private String returnFlightNumber;
    private String returnDepartureTime;
    private String returnArrivalTime;
    private String returnDuration;
    private Integer returnTransferCount;
    private Boolean returnIsDirectFlight;
    private String returnDepartureAirportCode;
    private String returnArrivalAirportCode;
    private String returnDepartureCityCode;
    private String returnArrivalCityCode;
    
    // 总体信息
    private Integer totalTransferCount;
    private Boolean isAllDirectFlight;
    private String cabinClass;
    private String baggageInfo;
    private String purchaseNotes;
    
    private String dataFingerprint;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    /**
     * 从RoundTripFlightInfo创建实体
     */
    public static RoundTripFlightData fromRoundTripFlightInfo(RoundTripFlightInfo flight, String transactionId, String searchRoute) {
        RoundTripFlightData data = new RoundTripFlightData();
        
        data.setTransactionId(transactionId);
        data.setSearchRoute(searchRoute);
        data.setItineraryId(flight.getItineraryId());

        // 价格信息
        data.setAdultTotalPrice(flight.getAdultTotalPrice());
        data.setChildTotalPrice(flight.getChildTotalPrice());
        data.setInfantTotalPrice(flight.getInfantTotalPrice());
        data.setCurrency(flight.getCurrency());
        data.setMiseryIndex(flight.getMiseryIndex());
        data.setTicketCount(flight.getTicketCount());
        
        // 去程信息
        OneWayFlightInfo outbound = flight.getOutboundFlight();
        if (outbound != null) {
            data.setOutboundFlightNumber(outbound.getFlightNumber());
            data.setOutboundDepartureTime(outbound.getDepartureTime().toString());
            data.setOutboundArrivalTime(outbound.getArrivalTime().toString());
            data.setOutboundDuration(outbound.getDuration());
            data.setOutboundTransferCount(outbound.getTransferCount());
            data.setOutboundIsDirectFlight(outbound.getIsDirectFlight());
            data.setOutboundDepartureAirportCode(outbound.getDepartureAirportCode());
            data.setOutboundArrivalAirportCode(outbound.getArrivalAirportCode());
            data.setOutboundDepartureCityCode(outbound.getDepartureCityCode());
            data.setOutboundArrivalCityCode(outbound.getArrivalCityCode());
        }
        
        // 返程信息
        OneWayFlightInfo returnFlight = flight.getReturnFlight();
        if (returnFlight != null) {
            data.setReturnFlightNumber(returnFlight.getFlightNumber());
            data.setReturnDepartureTime(returnFlight.getDepartureTime().toString());
            data.setReturnArrivalTime(returnFlight.getArrivalTime().toString());
            data.setReturnDuration(returnFlight.getDuration());
            data.setReturnTransferCount(returnFlight.getTransferCount());
            data.setReturnIsDirectFlight(returnFlight.getIsDirectFlight());
            data.setReturnDepartureAirportCode(returnFlight.getDepartureAirportCode());
            data.setReturnArrivalAirportCode(returnFlight.getArrivalAirportCode());
            data.setReturnDepartureCityCode(returnFlight.getDepartureCityCode());
            data.setReturnArrivalCityCode(returnFlight.getArrivalCityCode());
        }
        
        // 总体信息
        data.setTotalTransferCount(flight.getTotalTransferCount());
        data.setIsAllDirectFlight(flight.getIsAllDirectFlight());
        data.setCabinClass(flight.getCabinClass());
        data.setBaggageInfo(flight.getBaggageInfo());
        
        // 处理购买须知
        if (flight.getPurchaseNotes() != null) {
            data.setPurchaseNotes(String.join("; ", flight.getPurchaseNotes()));
        }
        
        // 生成数据指纹用于去重
        data.setDataFingerprint(generateFingerprint(flight));
        data.setCreatedAt(LocalDateTime.now());
        data.setUpdatedAt(LocalDateTime.now());
        
        return data;
    }

    /**
     * 生成数据指纹
     */
    private static String generateFingerprint(RoundTripFlightInfo flight) {
        StringBuilder sb = new StringBuilder();
        
        if (flight.getOutboundFlight() != null) {
            OneWayFlightInfo outbound = flight.getOutboundFlight();
            sb.append("OUT:").append(outbound.getFlightNumber()).append("|")
              .append(outbound.getDepartureTime()).append("|")
              .append(outbound.getArrivalTime()).append("|");
        }
        
        if (flight.getReturnFlight() != null) {
            OneWayFlightInfo returnFlight = flight.getReturnFlight();
            sb.append("RET:").append(returnFlight.getFlightNumber()).append("|")
              .append(returnFlight.getDepartureTime()).append("|")
              .append(returnFlight.getArrivalTime()).append("|");
        }
        
        sb.append(flight.getCabinClass()).append("|")
          .append(flight.getAdultTotalPrice());
        
        return sb.toString().hashCode() + "";
    }
}